
package io.adminboot.web;

import io.adminboot.entity.SysMenu;
import io.adminboot.exception.MyException;
import io.adminboot.service.ShiroService;
import io.adminboot.service.SysMenuService;
import io.adminboot.system.annotation.AddSysLog;
import io.adminboot.utils.Constant;
import io.adminboot.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 系统菜单
 * 
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private ShiroService shiroService;

	/**
	 * 导航菜单
	 */
	@GetMapping("/nav")
	public R nav() {
		List<SysMenu> menuList = sysMenuService.getUserMenuList(getUserId());
		Set<String> permissions = shiroService.getUserPermissions(getUserId());
		return R.ok().put("menuList", menuList).put("permissions", permissions);
	}

	/**
	 * 所有菜单列表
	 */
	@GetMapping("/list")
//	@RequiresPermissions("sys:menu:list")
	public List<SysMenu> list() {
		List<SysMenu> menuList = sysMenuService.queryAll();
		for (SysMenu sysMenuEntity : menuList) {
			SysMenu parentMenuEntity = sysMenuService.selectById(sysMenuEntity.getParentId());
			if (parentMenuEntity != null) {
				sysMenuEntity.setParentName(parentMenuEntity.getName());
			}
		}

		return menuList;
	}

	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/select")
//	@RequiresPermissions("sys:menu:select")
	public R select() {
		// 查询列表数据
		List<SysMenu> menuList = sysMenuService.queryNotButtonList();

		// 添加顶级菜单
		SysMenu root = new SysMenu();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);

		return R.ok().put("menuList", menuList);
	}

	/**
	 * 菜单信息
	 */
	@GetMapping("/info/{menuId}")
//	@RequiresPermissions("sys:menu:info")
	public R info(@PathVariable("menuId") Long menuId) {
		SysMenu menu = sysMenuService.selectById(menuId);
		return R.ok().put("menu", menu);
	}

	/**
	 * 保存
	 */
	@AddSysLog("保存菜单")
	@PostMapping("/save")
//	@RequiresPermissions("sys:menu:save")
	public R save(@RequestBody SysMenu menu) {
		// 数据校验
		verifyForm(menu);

		sysMenuService.insert(menu);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@AddSysLog("修改菜单")
	@PostMapping("/update")
//	@RequiresPermissions("sys:menu:update")
	public R update(@RequestBody SysMenu menu) {
		// 数据校验
		verifyForm(menu);

		sysMenuService.updateById(menu);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@AddSysLog("删除菜单")
	@PostMapping("/delete/{menuId}")
//	@RequiresPermissions("sys:menu:delete")
	public R delete(@PathVariable("menuId") long menuId) {
		if (menuId <= 31) {
			return R.alert("系统菜单，不能删除");
		}

		// 判断是否有子菜单或按钮
		List<SysMenu> menuList = sysMenuService.queryListParentId(menuId);
		if (menuList.size() > 0) {
			return R.alert("请先删除子菜单或按钮");
		}

		sysMenuService.delete(menuId);

		return R.ok();
	}

	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenu menu) {
		if (StringUtils.isBlank(menu.getName())) {
			throw new MyException("菜单名称不能为空");
		}

		if (menu.getParentId() == null) {
			throw new MyException("上级菜单不能为空");
		}

		// 菜单
		if (menu.getType() == Constant.MenuType.MENU.getValue()) {
			if (StringUtils.isBlank(menu.getUrl())) {
				throw new MyException("菜单URL不能为空");
			}
		}

		// 上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if (menu.getParentId() != 0) {
			SysMenu parentMenu = sysMenuService.selectById(menu.getParentId());
			parentType = parentMenu.getType();
		}

		// 目录、菜单
		if (menu.getType() == Constant.MenuType.CATALOG.getValue()
				|| menu.getType() == Constant.MenuType.MENU.getValue()) {
			if (parentType != Constant.MenuType.CATALOG.getValue()) {
				throw new MyException("上级菜单只能为目录类型");
			}
			return;
		}

		// 按钮
		if (menu.getType() == Constant.MenuType.BUTTON.getValue()) {
			if (parentType != Constant.MenuType.MENU.getValue()) {
				throw new MyException("上级菜单只能为菜单类型");
			}
			return;
		}
	}
}
